<?php

/**
 * @author Nguyen Thai Binh
 */
class Permission extends CI_Controller {

  protected $class;

  public function __construct() {
    parent::__construct();
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $this->class = $this->router->fetch_class();
    set_userdata('currentURL', current_url());
  }

  public function index() {
    $temp = $this->group_model->get_all();
    $user_group = array();
    foreach ($temp as $g) {
      $user_group[$g['id']]['name'] = $g['name'];
      $user_group[$g['id']]['access'] = '';
      $user_group[$g['id']]['deny'] = '';
    }
    $user_group[0]['name'] = 'GUEST';
    $user_group[0]['access'] = '';
    $user_group[0]['deny'] = '';
    $objs = $this->permission_model->get_all();
    foreach ($objs as $obj) {
      $user_group[$obj['group_id']]['access'] = $obj['access'];
      $user_group[$obj['group_id']]['deny'] = $obj['deny'];
    }
    $this->load->view('admin/permission/list', array('objs' => $user_group));
  }

  public function save() {
    if (!empty($_POST['group'])) {
      $this->permission_model->truncate();
      foreach ($_POST['group'] as $id => $p) {
        $this->permission_model->save(array('group_id' => $id, 'access' => isset($p['access']) ? $p['access'] : '',
            'deny' => isset($p['deny']) ? $p['deny'] : ''));
      }
      set_flash_message($this->lang->line('admin.permission.save.success'));
    }
    redirect('admin/permission');
  }

}